﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;
using System.Data.SqlTypes;

namespace IHISCETeim.DAL
{
    public  class OMarketQualificationDAL
    {
        public static int Add(OMarketQualificationInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OMarketQualification_Add", obj.ItemCode, obj.ItemName, obj.SupplerName, obj.MachineType, obj.SerialNumber, obj.AgentName, obj.CustomerName, obj.CustomerMobile, obj.InsertDate, obj.InsertHour,obj.VerifyRemark,obj.OldItemStatus,obj.State);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }

        public static int AddDetail(OMarketQualificationDetailInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OMarketQualification_AddDetail", obj.MQID, obj.SurveyDate, obj.SurveyHour, obj.SurveyState, obj.SurveyRemark);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }

        public static bool Update(OMarketQualificationInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OMarketQualification_Update", obj.MQID, obj.ItemCode, obj.ItemName, obj.SupplerName, obj.MachineType, obj.SerialNumber, obj.AgentName, obj.CustomerName, obj.CustomerMobile, obj.InsertDate, obj.InsertHour, obj.TestHour, obj.VerifyRemark,obj.OldItemStatus, obj.State);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }

        public static bool Delete(int MQID)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            try
            {
                DbCommand dbCommand = DB.GetStoredProcCommand("OMarketQualification_Delete", MQID);
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static IDataReader GetByID(int MQID)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("OMarketQualification_GetByID", MQID);
            return DB.ExecuteReader(dbCommand);
	    }

        public static IDataReader GetByItemCode(string ItemCode)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("OMarketQualification_GetByItemCode", ItemCode);
            return DB.ExecuteReader(dbCommand);
	    }

        public static IDataReader GetDetailByMQID(int MQID)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("OMarketQualification_GetDetailByMQID", MQID);
            return DB.ExecuteReader(dbCommand);
	    }

        public static DataTable GetPagination(string WhereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("OMarketQualification_GetPagination");
            DB.AddInParameter(dbCommand, "WhereSql", DbType.String,WhereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32,PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32,PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String,Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32,8);
			DataTable dt=  DB.ExecuteDataSet(dbCommand).Tables[0];
				
            TotalCount=Convert.ToInt32( DB.GetParameterValue(dbCommand,"TotalCount"));
            return dt;
	    }
    }
}
